package com.xuhai.blog19.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xuhai.blog19.admin.controller.AbstractBaseController;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author PangJunjie
 * @Date 2023/2/8/008
 */
@Component
public class MyMetaObjectHandler extends AbstractBaseController implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = getFieldValByName("createTime", metaObject);
        if (createTime == null) {
            setFieldValByName("createTime", new Date(), metaObject);
        }
        Object createAdmin = getFieldValByName("createAdmin", metaObject);
        if (createAdmin == null) {
            setFieldValByName("createAdmin", getUserId(), metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (updateTime == null) {
            setFieldValByName("updateTime", new Date(), metaObject);
        }
        Object updateAdmin = getFieldValByName("updateAdmin", metaObject);
        if (updateAdmin == null) {
            setFieldValByName("updateAdmin", getUserId(), metaObject);
        }
    }
}
